package reflect;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

/**
 * 实例化与当前类Test2在同一个包中的所有类
 * 用无参构造器实例化
 */
public class Test2 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        /*
        固定写法，使用哪个类就是定位该类的字节码文件所在的目录
         */
        File dir=new File(
                Test2.class.getResource(".").toURI()
        );
       String packname =Test2.class.getPackage().getName();
        System.out.println(packname);

        File[] subs = dir.listFiles(f ->f.getName().endsWith(".class"));
        for (File sub:subs){
            String fileName = sub.getName();
            String className=fileName.substring(0,fileName.indexOf("."));
            System.out.println(className);
            Class cls=Class.forName(packname+"."+className);
            Object obj = cls.newInstance();
            System.out.println(obj);
        }

    }
}
